<template>
  <div class="router-demo-container">
    <h1>路由信息展示页面</h1>
    <el-card class="route-card">
      <template #header>
        <div class="card-header">
          <span>当前路由信息</span>
        </div>
      </template>
      <div class="route-info">
        <el-descriptions :column="1" border>
          <el-descriptions-item label="路由路径">{{ $route.path }}</el-descriptions-item>
          <el-descriptions-item label="路由名称">{{ $route.name || '无' }}</el-descriptions-item>
          <el-descriptions-item label="路由参数">{{ JSON.stringify($route.params) }}</el-descriptions-item>
          <el-descriptions-item label="查询参数">{{ JSON.stringify($route.query) }}</el-descriptions-item>
        </el-descriptions>
      </div>
    </el-card>

    <el-card class="route-card">
      <template #header>
        <div class="card-header">
          <span>所有路由列表</span>
        </div>
      </template>
      <div class="route-list">
        <el-table :data="allRoutes" style="width: 100%" stripe>
          <el-table-column prop="path" label="路径" width="200" />
          <el-table-column prop="name" label="名称" width="150" />
          <el-table-column prop="meta.title" label="标题" width="150" />
          <el-table-column prop="component" label="组件" />
          <el-table-column label="操作" width="120">
            <template #default="scope">
              <el-button size="small" @click="goToRoute(scope.row)">跳转</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'

// 获取路由对象
const $route = useRoute()
const $router = useRouter()

// 存储所有路由
const allRoutes = ref<any[]>([])

// 获取所有路由信息
const getAllRoutes = () => {
  // 获取路由器中的所有路由
  const routes = $router.getRoutes()
  
  // 过滤掉一些不必要的路由
  const filteredRoutes = routes.filter(route => {
    // 过滤掉空路径和重定向路由
    return route.path && route.path !== '/' && !route.redirect
  })
  
  // 提取需要展示的信息
  allRoutes.value = filteredRoutes.map(route => ({
    path: route.path,
    name: route.name,
    meta: route.meta,
    component: route.components ? Object.keys(route.components)[0] : '无'
  }))
}

// 跳转到指定路由
const goToRoute = (route: any) => {
  if (route.path) {
    $router.push(route.path)
  }
}

// 组件挂载时获取路由信息
onMounted(() => {
  getAllRoutes()
})
</script>

<style scoped lang="scss">
.router-demo-container {
  padding: 20px;
  background-color: rgba(255, 255, 255, 0.8);
  border-radius: 10px;
  min-height: calc(100vh - 120px);
}

.route-card {
  margin-bottom: 20px;
}

.card-header {
  font-weight: bold;
  font-size: 16px;
}

.route-info {
  margin-top: 20px;
}

.route-list {
  margin-top: 20px;
}
</style>